package com.daiqile.myinterest.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.daiqile.myinterest.R;
import com.daiqile.myinterest.customview.ObservableScrollView;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public class FirstActivity extends AppCompatActivity implements ObservableScrollView.OnObservableScrollViewScrollChanged {
    private ObservableScrollView sv_contentView;
    private LinearLayout ll_topView;
    private LinearLayout tv_topView;
    private LinearLayout ll_fixedView;
    private MZBannerView bannerView;
    private SearchView mSearchView;
    private SearchView.SearchAutoComplete mSearchAutoComplete;
    //用来记录内层固定布局到屏幕顶部的距离
    private int mHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        ButterKnife.bind(this);
        sv_contentView = (ObservableScrollView) findViewById(R.id.sv_contentView);
        ll_topView = (LinearLayout) findViewById(R.id.ll_topView);
        tv_topView = (LinearLayout) findViewById(R.id.tv_topView);
        ll_fixedView = (LinearLayout) findViewById(R.id.ll_fixedView);
        bannerView = (MZBannerView) findViewById(R.id.banner_pic);
        sv_contentView.setOnObservableScrollViewScrollChanged(this);
        mSearchView = (SearchView) findViewById(R.id.search);
        mSearchAutoComplete = (SearchView.SearchAutoComplete) mSearchView.findViewById(R.id.search_src_text);
        mSearchAutoComplete.setHintTextColor(getResources().getColor(android.R.color.white));//设置提示文字颜色
        mSearchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));//设置内容文字颜色
        mSearchView.setQueryHint("输入歌曲名查找");
        mSearchView.onActionViewExpanded();// 当展开无输入内容的时候，没有关闭的图标
        initBanner();

    }

    private void initBanner() {
        List<String> urlList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            urlList.add("http://47.97.61.223/grass/upload/ppt/img/20171117/20171117164106339297.png");
        }
        bannerView.setPages(urlList, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        bannerView.start();
    }

    public static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int i, String url) {
            Glide.with(context)
                    .load(url)
                    .error(R.drawable.err)
                    .into(mImageView);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            //获取HeaderView的高度，当滑动大于等于这个高度的时候，需要把topView移除当前布局，放入到外层布局
            mHeight = ll_topView.getTop();
            Log.d("mHeight",String.valueOf(mHeight));
        }
    }

    /**
     * @param l    Current horizontal scroll origin. 当前滑动的x轴距离
     * @param t    Current vertical scroll origin. 当前滑动的y轴距离
     * @param oldl Previous horizontal scroll origin. 上一次滑动的x轴距离
     * @param oldt Previous vertical scroll origin. 上一次滑动的y轴距离
     */

    @Override
    public void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt) {
        if (t >= mHeight) {
            if (tv_topView.getParent() != ll_fixedView) {
                ll_topView.removeView(tv_topView);
                ll_fixedView.addView(tv_topView);
            }
            } else {
                    if (tv_topView.getParent() != ll_topView) {
                        ll_fixedView.removeView(tv_topView);
                        ll_topView.addView(tv_topView);
                        }
            }
    }

}

